package org.topic.discount;

import org.topic.user.ShoopingCart;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 组合折扣策略
 */
public class CompositeDiscount implements DiscountStrategy {

    private List<DiscountStrategy> strategies = new ArrayList<>();

    public void addStrategy(DiscountStrategy strategy) {
        strategies.add(strategy);
    }

    @Override
    public BigDecimal applyDiscount(BigDecimal subtotal, ShoopingCart cart) {

        BigDecimal currentTotal = subtotal;

        // 按顺序应用所有折扣策略
        for (DiscountStrategy strategy : strategies) {
            currentTotal = strategy.applyDiscount(currentTotal, cart);
        }

        return currentTotal;
    }
}
